import cn.hutool.core.date.DateUtil;

import java.security.SecureRandom;
import java.time.Instant;
import java.util.Random;

/**
 * @author zhangdi
 * @version 1.0
 * @date 2024.8.16
 */
public class AppKeyGen {
    private static final String ALPHANUMERIC = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private static final SecureRandom SECURE_RANDOM = new SecureRandom();
    private static final Random RANDOM = new Random();

    public static String generateAppKey(boolean isSecure) {
        // 与appId类似，但长度更长
        long timestamp = DateUtil.current(); // 允许更长的时间戳部分
        System.out.println(timestamp);
        StringBuilder sb = new StringBuilder(Long.toString(timestamp, 36).toUpperCase());
        System.out.println(sb.toString());

        // 填充剩余部分
        while (sb.length() < 32) {
            int randomIndex = isSecure ? SECURE_RANDOM.nextInt(ALPHANUMERIC.length()) : RANDOM.nextInt(ALPHANUMERIC.length());
            sb.append(ALPHANUMERIC.charAt(randomIndex));
        }

        if (sb.length() > 32) {
            sb.setLength(32);
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(AppKeyGen.generateAppKey(true));
    }
}
